home *** CD-ROM | disk | FTP | other *** search
/ Aminet 40 / Aminet 40 (2000)(Schatztruhe)[!][Dec 2000].iso / Aminet / dev / lang / Python16_Src.lha / Python16_Source / Modules / shamodule.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-09-10  |  15.2 KB  |  576 lines

  1. /* SHA module */
  2.  
  3. /* This module provides an interface to NIST's Secure Hash Algorithm */
  4.  
  5. /* See below for information about the original code this module was
  6.    based upon. Additional work performed by:
  7.  
  8.    Andrew Kuchling (amk1@erols.com)
  9.    Greg Stein (gstein@lyra.org)
  10. */
  11.  
  12. /* SHA objects */
  13.  
  14. #include "Python.h"
  15.  
  16.  
  17. /* Endianness testing and definitions */
  18. #define TestEndianness(variable) {int i=1; variable=PCT_BIG_ENDIAN;\
  19.     if (*((char*)&i)==1) variable=PCT_LITTLE_ENDIAN;}
  20.  
  21. #define PCT_LITTLE_ENDIAN 1
  22. #define PCT_BIG_ENDIAN 0
  23.  
  24. /* Some useful types */
  25.  
  26. typedef unsigned char SHA_BYTE;
  27.  
  28. #if SIZEOF_INT == 4
  29. typedef unsigned int SHA_INT32;    /* 32-bit integer */
  30. #else
  31. /* not defined. compilation will die. */
  32. #endif
  33.  
  34. /* The SHA block size and message digest sizes, in bytes */
  35.  
  36. #define SHA_BLOCKSIZE    64
  37. #define SHA_DIGESTSIZE  20
  38.  
  39. /* The structure for storing SHS info */
  40.  
  41. typedef struct {
  42.     PyObject_HEAD
  43.     SHA_INT32 digest[5];        /* Message digest */
  44.     SHA_INT32 count_lo, count_hi;    /* 64-bit bit count */
  45.     SHA_BYTE data[SHA_BLOCKSIZE];    /* SHA data buffer */
  46.     int Endianness;
  47.     int local;            /* unprocessed amount in data */
  48. } SHAobject;
  49.  
  50. #include "protos/shamodule.h"
  51.  
  52. /* When run on a little-endian CPU we need to perform byte reversal on an
  53.    array of longwords. */
  54.  
  55. static void longReverse(buffer, byteCount, Endianness)
  56.     SHA_INT32 *buffer; 
  57.     int byteCount, Endianness;
  58. {
  59.     SHA_INT32 value;
  60.  
  61.     if ( Endianness == PCT_BIG_ENDIAN )
  62.     return;
  63.  
  64.     byteCount /= sizeof(*buffer);
  65.     while( byteCount-- )
  66.     {
  67.         value = *buffer;
  68.         value = ( ( value & 0xFF00FF00L ) >> 8  ) | \
  69.                 ( ( value & 0x00FF00FFL ) << 8 );
  70.         *buffer++ = ( value << 16 ) | ( value >> 16 );
  71.     }
  72. }
  73.  
  74. static void SHAcopy(src, dest)
  75.      SHAobject *src, *dest;
  76. {
  77.     dest->Endianness = src->Endianness;
  78.     dest->local = src->local;
  79.     dest->count_lo = src->count_lo;
  80.     dest->count_hi = src->count_hi;
  81.     memcpy(dest->digest, src->digest, sizeof(src->digest));
  82.     memcpy(dest->data, src->data, sizeof(src->data));
  83. }
  84.  
  85.  
  86. /* ------------------------------------------------------------------------
  87.  *
  88.  * This code for the SHA algorithm was noted as public domain. The original
  89.  * headers are pasted below.
  90.  *
  91.  * Several changes have been made to make it more compatible with the
  92.  * Python environment and desired interface.
  93.  *
  94.  */
  95.  
  96. /* NIST Secure Hash Algorithm */
  97. /* heavily modified by Uwe Hollerbach <uh@alumni.caltech edu> */
  98. /* from Peter C. Gutmann's implementation as found in */
  99. /* Applied Cryptography by Bruce Schneier */
  100. /* Further modifications to include the "UNRAVEL" stuff, below */
  101.  
  102. /* This code is in the public domain */
  103.  
  104. /* UNRAVEL should be fastest & biggest */
  105. /* UNROLL_LOOPS should be just as big, but slightly slower */
  106. /* both undefined should be smallest and slowest */
  107.  
  108. #define UNRAVEL
  109. /* #define UNROLL_LOOPS */
  110.  
  111. /* The SHA f()-functions.  The f1 and f3 functions can be optimized to
  112.    save one boolean operation each - thanks to Rich Schroeppel,
  113.    rcs@cs.arizona.edu for discovering this */
  114.  
  115. /*#define f1(x,y,z)    ((x & y) | (~x & z))        // Rounds  0-19 */
  116. #define f1(x,y,z)    (z ^ (x & (y ^ z)))        /* Rounds  0-19 */
  117. #define f2(x,y,z)    (x ^ y ^ z)            /* Rounds 20-39 */
  118. /*#define f3(x,y,z)    ((x & y) | (x & z) | (y & z))    // Rounds 40-59 */
  119. #define f3(x,y,z)    ((x & y) | (z & (x | y)))    /* Rounds 40-59 */
  120. #define f4(x,y,z)    (x ^ y ^ z)            /* Rounds 60-79 */
  121.  
  122. /* SHA constants */
  123.  
  124. #define CONST1        0x5a827999L            /* Rounds  0-19 */
  125. #define CONST2        0x6ed9eba1L            /* Rounds 20-39 */
  126. #define CONST3        0x8f1bbcdcL            /* Rounds 40-59 */
  127. #define CONST4        0xca62c1d6L            /* Rounds 60-79 */
  128.  
  129. /* 32-bit rotate */
  130.  
  131. #define R32(x,n)    ((x << n) | (x >> (32 - n)))
  132.  
  133. /* the generic case, for when the overall rotation is not unraveled */
  134.  
  135. #define FG(n)    \
  136.     T = R32(A,5) + f##n(B,C,D) + E + *WP++ + CONST##n;    \
  137.     E = D; D = C; C = R32(B,30); B = A; A = T
  138.  
  139. /* specific cases, for when the overall rotation is unraveled */
  140.  
  141. #define FA(n)    \
  142.     T = R32(A,5) + f##n(B,C,D) + E + *WP++ + CONST##n; B = R32(B,30)
  143.  
  144. #define FB(n)    \
  145.     E = R32(T,5) + f##n(A,B,C) + D + *WP++ + CONST##n; A = R32(A,30)
  146.  
  147. #define FC(n)    \
  148.     D = R32(E,5) + f##n(T,A,B) + C + *WP++ + CONST##n; T = R32(T,30)
  149.  
  150. #define FD(n)    \
  151.     C = R32(D,5) + f##n(E,T,A) + B + *WP++ + CONST##n; E = R32(E,30)
  152.  
  153. #define FE(n)    \
  154.     B = R32(C,5) + f##n(D,E,T) + A + *WP++ + CONST##n; D = R32(D,30)
  155.  
  156. #define FT(n)    \
  157.     A = R32(B,5) + f##n(C,D,E) + T + *WP++ + CONST##n; C = R32(C,30)
  158.  
  159. /* do SHA transformation */
  160.  
  161. static void
  162. sha_transform(sha_info)
  163.     SHAobject *sha_info;
  164. {
  165.     int i;
  166.     SHA_INT32 T, A, B, C, D, E, W[80], *WP;
  167.  
  168.     memcpy(W, sha_info->data, sizeof(sha_info->data));
  169.     longReverse(W, (int)sizeof(sha_info->data), sha_info->Endianness);
  170.  
  171.     for (i = 16; i < 80; ++i) {
  172.     W[i] = W[i-3] ^ W[i-8] ^ W[i-14] ^ W[i-16];
  173.  
  174.     /* extra rotation fix */
  175.     W[i] = R32(W[i], 1);
  176.     }
  177.     A = sha_info->digest[0];
  178.     B = sha_info->digest[1];
  179.     C = sha_info->digest[2];
  180.     D = sha_info->digest[3];
  181.     E = sha_info->digest[4];
  182.     WP = W;
  183. #ifdef UNRAVEL
  184.     FA(1); FB(1); FC(1); FD(1); FE(1); FT(1); FA(1); FB(1); FC(1); FD(1);
  185.     FE(1); FT(1); FA(1); FB(1); FC(1); FD(1); FE(1); FT(1); FA(1); FB(1);
  186.     FC(2); FD(2); FE(2); FT(2); FA(2); FB(2); FC(2); FD(2); FE(2); FT(2);
  187.     FA(2); FB(2); FC(2); FD(2); FE(2); FT(2); FA(2); FB(2); FC(2); FD(2);
  188.     FE(3); FT(3); FA(3); FB(3); FC(3); FD(3); FE(3); FT(3); FA(3); FB(3);
  189.     FC(3); FD(3); FE(3); FT(3); FA(3); FB(3); FC(3); FD(3); FE(3); FT(3);
  190.     FA(4); FB(4); FC(4); FD(4); FE(4); FT(4); FA(4); FB(4); FC(4); FD(4);
  191.     FE(4); FT(4); FA(4); FB(4); FC(4); FD(4); FE(4); FT(4); FA(4); FB(4);
  192.     sha_info->digest[0] += E;
  193.     sha_info->digest[1] += T;
  194.     sha_info->digest[2] += A;
  195.     sha_info->digest[3] += B;
  196.     sha_info->digest[4] += C;
  197. #else /* !UNRAVEL */
  198. #ifdef UNROLL_LOOPS
  199.     FG(1); FG(1); FG(1); FG(1); FG(1); FG(1); FG(1); FG(1); FG(1); FG(1);
  200.     FG(1); FG(1); FG(1); FG(1); FG(1); FG(1); FG(1); FG(1); FG(1); FG(1);
  201.     FG(2); FG(2); FG(2); FG(2); FG(2); FG(2); FG(2); FG(2); FG(2); FG(2);
  202.     FG(2); FG(2); FG(2); FG(2); FG(2); FG(2); FG(2); FG(2); FG(2); FG(2);
  203.     FG(3); FG(3); FG(3); FG(3); FG(3); FG(3); FG(3); FG(3); FG(3); FG(3);
  204.     FG(3); FG(3); FG(3); FG(3); FG(3); FG(3); FG(3); FG(3); FG(3); FG(3);
  205.     FG(4); FG(4); FG(4); FG(4); FG(4); FG(4); FG(4); FG(4); FG(4); FG(4);
  206.     FG(4); FG(4); FG(4); FG(4); FG(4); FG(4); FG(4); FG(4); FG(4); FG(4);
  207. #else /* !UNROLL_LOOPS */
  208.     for (i =  0; i < 20; ++i) { FG(1); }
  209.     for (i = 20; i < 40; ++i) { FG(2); }
  210.     for (i = 40; i < 60; ++i) { FG(3); }
  211.     for (i = 60; i < 80; ++i) { FG(4); }
  212. #endif /* !UNROLL_LOOPS */
  213.     sha_info->digest[0] += A;
  214.     sha_info->digest[1] += B;
  215.     sha_info->digest[2] += C;
  216.     sha_info->digest[3] += D;
  217.     sha_info->digest[4] += E;
  218. #endif /* !UNRAVEL */
  219. }
  220.  
  221. /* initialize the SHA digest */
  222.  
  223. static void
  224. sha_init(sha_info)
  225.     SHAobject *sha_info;
  226. {
  227.     TestEndianness(sha_info->Endianness)
  228.  
  229.     sha_info->digest[0] = 0x67452301L;
  230.     sha_info->digest[1] = 0xefcdab89L;
  231.     sha_info->digest[2] = 0x98badcfeL;
  232.     sha_info->digest[3] = 0x10325476L;
  233.     sha_info->digest[4] = 0xc3d2e1f0L;
  234.     sha_info->count_lo = 0L;
  235.     sha_info->count_hi = 0L;
  236.     sha_info->local = 0;
  237. }
  238.  
  239. /* update the SHA digest */
  240.  
  241. static void
  242. sha_update(sha_info, buffer, count)
  243.     SHAobject *sha_info;
  244.     SHA_BYTE *buffer;
  245.     int count;
  246. {
  247.     int i;
  248.     SHA_INT32 clo;
  249.  
  250.     clo = sha_info->count_lo + ((SHA_INT32) count << 3);
  251.     if (clo < sha_info->count_lo) {
  252.         ++sha_info->count_hi;
  253.     }
  254.     sha_info->count_lo = clo;
  255.     sha_info->count_hi += (SHA_INT32) count >> 29;
  256.     if (sha_info->local) {
  257.         i = SHA_BLOCKSIZE - sha_info->local;
  258.         if (i > count) {
  259.             i = count;
  260.         }
  261.         memcpy(((SHA_BYTE *) sha_info->data) + sha_info->local,
  262.                buffer, i);
  263.         count -= i;
  264.         buffer += i;
  265.         sha_info->local += i;
  266.         if (sha_info->local == SHA_BLOCKSIZE) {
  267.             sha_transform(sha_info);
  268.         } else {
  269.             return;
  270.         }
  271.     }
  272.     while (count >= SHA_BLOCKSIZE) {
  273.         memcpy(sha_info->data, buffer, SHA_BLOCKSIZE);
  274.         buffer += SHA_BLOCKSIZE;
  275.         count -= SHA_BLOCKSIZE;
  276.         sha_transform(sha_info);
  277.     }
  278.     memcpy(sha_info->data, buffer, count);
  279.     sha_info->local = count;
  280. }
  281.  
  282. /* finish computing the SHA digest */
  283.  
  284. static void
  285. sha_final(digest, sha_info)
  286.     unsigned char digest[20];
  287.     SHAobject *sha_info;
  288. {
  289.     int count;
  290.     SHA_INT32 lo_bit_count, hi_bit_count;
  291.  
  292.     lo_bit_count = sha_info->count_lo;
  293.     hi_bit_count = sha_info->count_hi;
  294.     count = (int) ((lo_bit_count >> 3) & 0x3f);
  295.     ((SHA_BYTE *) sha_info->data)[count++] = 0x80;
  296.     if (count > SHA_BLOCKSIZE - 8)
  297.     {
  298.     memset(((SHA_BYTE *) sha_info->data) + count, 0,
  299.            SHA_BLOCKSIZE - count);
  300.     sha_transform(sha_info);
  301.     memset((SHA_BYTE *) sha_info->data, 0, SHA_BLOCKSIZE - 8);
  302.     }
  303.     else
  304.     {
  305.     memset(((SHA_BYTE *) sha_info->data) + count, 0,
  306.            SHA_BLOCKSIZE - 8 - count);
  307.     }
  308.  
  309.     /* GJS: note that we add the hi/lo in big-endian. sha_transform will
  310.        swap these values into host-order. */
  311.     sha_info->data[56] = (hi_bit_count >> 24) & 0xff;
  312.     sha_info->data[57] = (hi_bit_count >> 16) & 0xff;
  313.     sha_info->data[58] = (hi_bit_count >>  8) & 0xff;
  314.     sha_info->data[59] = (hi_bit_count >>  0) & 0xff;
  315.     sha_info->data[60] = (lo_bit_count >> 24) & 0xff;
  316.     sha_info->data[61] = (lo_bit_count >> 16) & 0xff;
  317.     sha_info->data[62] = (lo_bit_count >>  8) & 0xff;
  318.     sha_info->data[63] = (lo_bit_count >>  0) & 0xff;
  319.     sha_transform(sha_info);
  320.     digest[ 0] = (unsigned char) ((sha_info->digest[0] >> 24) & 0xff);
  321.     digest[ 1] = (unsigned char) ((sha_info->digest[0] >> 16) & 0xff);
  322.     digest[ 2] = (unsigned char) ((sha_info->digest[0] >>  8) & 0xff);
  323.     digest[ 3] = (unsigned char) ((sha_info->digest[0]      ) & 0xff);
  324.     digest[ 4] = (unsigned char) ((sha_info->digest[1] >> 24) & 0xff);
  325.     digest[ 5] = (unsigned char) ((sha_info->digest[1] >> 16) & 0xff);
  326.     digest[ 6] = (unsigned char) ((sha_info->digest[1] >>  8) & 0xff);
  327.     digest[ 7] = (unsigned char) ((sha_info->digest[1]      ) & 0xff);
  328.     digest[ 8] = (unsigned char) ((sha_info->digest[2] >> 24) & 0xff);
  329.     digest[ 9] = (unsigned char) ((sha_info->digest[2] >> 16) & 0xff);
  330.     digest[10] = (unsigned char) ((sha_info->digest[2] >>  8) & 0xff);
  331.     digest[11] = (unsigned char) ((sha_info->digest[2]      ) & 0xff);
  332.     digest[12] = (unsigned char) ((sha_info->digest[3] >> 24) & 0xff);
  333.     digest[13] = (unsigned char) ((sha_info->digest[3] >> 16) & 0xff);
  334.     digest[14] = (unsigned char) ((sha_info->digest[3] >>  8) & 0xff);
  335.     digest[15] = (unsigned char) ((sha_info->digest[3]      ) & 0xff);
  336.     digest[16] = (unsigned char) ((sha_info->digest[4] >> 24) & 0xff);
  337.     digest[17] = (unsigned char) ((sha_info->digest[4] >> 16) & 0xff);
  338.     digest[18] = (unsigned char) ((sha_info->digest[4] >>  8) & 0xff);
  339.     digest[19] = (unsigned char) ((sha_info->digest[4]      ) & 0xff);
  340. }
  341.  
  342. /*
  343.  * End of copied SHA code.
  344.  *
  345.  * ------------------------------------------------------------------------
  346.  */
  347.  
  348. staticforward PyTypeObject SHAtype;
  349.  
  350.  
  351. static SHAobject *
  352. newSHAobject()
  353. {
  354.     return (SHAobject *)PyObject_New(SHAobject, &SHAtype);
  355. }
  356.  
  357. /* Internal methods for a hashing object */
  358.  
  359. static void
  360. SHA_dealloc(ptr)
  361.     PyObject *ptr;
  362. {
  363.     PyObject_Del(ptr);
  364. }
  365.  
  366.  
  367. /* External methods for a hashing object */
  368.  
  369. static char SHA_copy__doc__[] = 
  370. "Return a copy of the hashing object.";
  371.  
  372. static PyObject *
  373. SHA_copy(self, args)
  374.     SHAobject *self;
  375.     PyObject *args;
  376. {
  377.     SHAobject *newobj;
  378.  
  379.     if (!PyArg_NoArgs(args)) {
  380.         return NULL;
  381.     }
  382.  
  383.     if ( (newobj = newSHAobject())==NULL)
  384.         return NULL;
  385.     
  386.     SHAcopy(self, newobj);
  387.     return (PyObject *)newobj;
  388. }
  389.  
  390. static char SHA_digest__doc__[] = 
  391. "Return the digest value as a string of binary data.";
  392.  
  393. static PyObject *
  394. SHA_digest(self, args)
  395.     SHAobject *self;
  396.     PyObject *args;
  397. {
  398.     unsigned char digest[SHA_DIGESTSIZE];
  399.     SHAobject temp;
  400.  
  401.     if (!PyArg_NoArgs(args))
  402.         return NULL;
  403.  
  404.     SHAcopy(self, &temp);
  405.     sha_final(digest, &temp);
  406.     return PyString_FromStringAndSize((const char *)digest, sizeof(digest));
  407. }
  408.  
  409. static char SHA_hexdigest__doc__[] = 
  410. "Return the digest value as a string of hexadecimal digits.";
  411.  
  412. static PyObject *
  413. SHA_hexdigest(self, args)
  414.     SHAobject *self;
  415.     PyObject *args;
  416. {
  417.     unsigned char digest[SHA_DIGESTSIZE];
  418.     SHAobject temp;
  419.     PyObject *retval;
  420.     char *hex_digest;
  421.     int i, j;
  422.  
  423.     if (!PyArg_NoArgs(args))
  424.         return NULL;
  425.  
  426.     /* Get the raw (binary) digest value */
  427.     SHAcopy(self, &temp);
  428.     sha_final(digest, &temp);
  429.  
  430.     /* Create a new string */
  431.     retval = PyString_FromStringAndSize(NULL, sizeof(digest) * 2);
  432.     hex_digest = PyString_AsString(retval);
  433.  
  434.     /* Make hex version of the digest */
  435.     for(i=j=0; i<sizeof(digest); i++)    
  436.     {
  437.         char c;
  438.         c = digest[i] / 16; c = (c>9) ? c+'a'-10 : c + '0';
  439.         hex_digest[j++] = c;
  440.         c = digest[i] % 16; c = (c>9) ? c+'a'-10 : c + '0';
  441.         hex_digest[j++] = c;
  442.     }
  443.  
  444.     return retval;
  445. }
  446.  
  447. static char SHA_update__doc__[] = 
  448. "Update this hashing object's state with the provided string.";
  449.  
  450. static PyObject *
  451. SHA_update(self, args)
  452.     SHAobject *self;
  453.     PyObject *args;
  454. {
  455.     unsigned char *cp;
  456.     int len;
  457.  
  458.     if (!PyArg_Parse(args, "s#", &cp, &len))
  459.         return NULL;
  460.  
  461.     sha_update(self, cp, len);
  462.  
  463.     Py_INCREF(Py_None);
  464.     return Py_None;
  465. }
  466.  
  467. static PyMethodDef SHA_methods[] = {
  468.     {"copy",    (PyCFunction)SHA_copy, 0, SHA_copy__doc__},
  469.     {"digest",    (PyCFunction)SHA_digest, 0, SHA_digest__doc__},
  470.     {"hexdigest",    (PyCFunction)SHA_hexdigest, 0, SHA_hexdigest__doc__},
  471.     {"update",    (PyCFunction)SHA_update, 0, SHA_update__doc__},
  472.     {NULL,        NULL}        /* sentinel */
  473. };
  474.  
  475. static PyObject *
  476. SHA_getattr(self, name)
  477.     PyObject *self;
  478.     char *name;
  479. {
  480.     if (strcmp(name, "blocksize")==0)
  481.         return PyInt_FromLong(1);
  482.     if (strcmp(name, "digestsize")==0)
  483.         return PyInt_FromLong(20);
  484.     
  485.     return Py_FindMethod(SHA_methods, self, name);
  486. }
  487.  
  488. static PyTypeObject SHAtype = {
  489.     PyObject_HEAD_INIT(NULL)
  490.     0,            /*ob_size*/
  491.     "SHA",            /*tp_name*/
  492.     sizeof(SHAobject),    /*tp_size*/
  493.     0,            /*tp_itemsize*/
  494.     /* methods */
  495.     SHA_dealloc,        /*tp_dealloc*/
  496.     0,            /*tp_print*/
  497.     SHA_getattr,        /*tp_getattr*/
  498. };
  499.  
  500.  
  501. /* The single module-level function: new() */
  502.  
  503. static char SHA_new__doc__[] =
  504.  "Return a new SHA hashing object.  An optional string "
  505.  "argument may be provided; if present, this string will be "
  506.  " automatically hashed."; 
  507.  
  508. static PyObject *
  509. SHA_new(self, args, kwdict)
  510.     PyObject *self;
  511.     PyObject *args;
  512.     PyObject *kwdict;
  513. {
  514.     static char *kwlist[] = {"string", NULL};
  515.     SHAobject *new;
  516.     unsigned char *cp = NULL;
  517.     int len;
  518.     
  519.     if ((new = newSHAobject()) == NULL)
  520.         return NULL;
  521.  
  522.     if (!PyArg_ParseTupleAndKeywords(args, kwdict, "|s#:new", kwlist,
  523.                      &cp, &len)) {
  524.             Py_DECREF(new);
  525.         return NULL;
  526.     }
  527.  
  528.         sha_init(new);
  529.  
  530.     if (PyErr_Occurred()) {
  531.         Py_DECREF(new);
  532.         return NULL;
  533.     }
  534.     if (cp)
  535.         sha_update(new, cp, len);
  536.  
  537.     return (PyObject *)new;
  538. }
  539.  
  540.  
  541. /* List of functions exported by this module */
  542.  
  543. static struct PyMethodDef SHA_functions[] = {
  544.     {"new", (PyCFunction)SHA_new, METH_VARARGS|METH_KEYWORDS, SHA_new__doc__},
  545.     {"sha",    (PyCFunction)SHA_new, METH_VARARGS|METH_KEYWORDS, SHA_new__doc__},
  546.     {NULL,    NULL}         /* Sentinel */
  547. };
  548.  
  549.  
  550. /* Initialize this module. */
  551.  
  552. #define insint(n,v) { PyObject *o=PyInt_FromLong(v); \
  553.     if (o!=NULL) PyDict_SetItemString(d,n,o); \
  554.     Py_XDECREF(o); }
  555.  
  556. void
  557. initsha()
  558. {
  559.     PyObject *d, *m;
  560.  
  561.     SHAtype.ob_type = &PyType_Type;
  562.     m = Py_InitModule("sha", SHA_functions);
  563.  
  564.     /* Add some symbolic constants to the module */
  565.     d = PyModule_GetDict(m);
  566.     insint("blocksize", 1);  /* For future use, in case some hash
  567.                     functions require an integral number of
  568.                     blocks */ 
  569.     insint("digestsize", 20);
  570.  
  571.     /* Check for errors */
  572.     if (PyErr_Occurred())
  573.         Py_FatalError("can't initialize module SHA");
  574. }
  575.  
  576.